package extend.db.entity.annotation;

import extend.db.entity.Entity;
import extend.db.entity.EntityReflectUtil;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 实体注解
 *<B>说明：</B>
 *<pre>
 * 略
 *</pre>
 *<B>示例：</B>
 *<pre>
 *  略
 *</pre>
 *<B>日志：</B>
 *<pre>
 *  略
 *</pre>
 *<B>注意事项：</B>
 *<pre>
 *  略
 *</pre>
 */
public class EntityAnnotation
{
    protected final static String FIELD_ANNOTATION_NAME = "field";
    protected final static String CLAZZ_ANNOTATION_NAME = "clazz";

    /**
     * 支持注解列表
     *<B>说明：</B>
     *<pre>
     *  略
     *</pre>
     */
    protected static List<Class> clazzAnnotationList = new ArrayList<Class>(){{
        add(Table.class);
    }};


    protected static List<Class> fieldAnnotationList = new ArrayList<Class>(){{
        add(Column.class);
        add(Id.class);
    }};

    /**
     * 解析注解
     *<B>说明：</B>
     *<pre>
     *  略
     *</pre>
     */
    public static void parseAnnotation(Class targetclazz) throws Exception
    {

        // 解析类注解
        parseClassAnnotation(targetclazz,clazzAnnotationList);

        // 解析属性注解
        parseFieldAnnotation(targetclazz);
    }

    /**
     * 解析类注解
     *<B>说明：</B>
     *<pre>
     *  略
     *</pre>
     */
    public static void parseClassAnnotation(Class targetclazz, List<Class> annotationList) throws Exception
    {
        Method processorMethod;
        Annotation[] annotations = targetclazz.getDeclaredAnnotations();
        if (annotations.length == 0) {
            return;
        }

        for (Annotation ann:annotations) {
            if (annotationList.contains(ann.annotationType())) {
                Class annotationProcessor = getAnnotationProcessor(ann);
                if (annotationProcessor != null) {
                    processorMethod = annotationProcessor.getMethod(CLAZZ_ANNOTATION_NAME, Class.class,Annotation.class);
                    processorMethod.invoke(annotationProcessor.newInstance(),targetclazz,ann);
                }
            }
        }
    }

    /**
     * 解析类字段注解
     *<B>说明：</B>
     *<pre>
     *  略
     *</pre>
     */
    public static void parseFieldAnnotation(Class targetClazz) throws Exception
    {
        // 获取类的所有属性
        Map<String,Field> attrs = EntityReflectUtil.getFields(targetClazz);
        Field field;

        // 遍历属性,获取注解
        for (Map.Entry<String,Field> attrsEntry : attrs.entrySet()) {
            field = attrsEntry.getValue();
            Map<String,Object> entityFieldAttrs = new HashMap<>();
            for (Class annClazz:fieldAnnotationList) {
                Annotation ann = field.getAnnotation(annClazz);
                if (ann != null) {
                    Map<String,Object> annAttrs = getAnnotationAttrs(ann);
                    // 合并数组
                    entityFieldAttrs.putAll(annAttrs);
                }
            }

            if (entityFieldAttrs.size() > 0) {
                EntityMetadata entityMetadata = Entity.getEntity(targetClazz);
                entityMetadata.setRelation(field,entityFieldAttrs);
            }
        }
    }


    /**
     * 获取注解的处理器
     *<B>说明：</B>
     *<pre>
     *  略
     *</pre>
     */
    public static Class<?> getAnnotationProcessor(Annotation annotation)
    {
        EntityProcessor processor = annotation.annotationType().getAnnotation(EntityProcessor.class);
        if (processor != null) {
            return processor.processor();
        }

        return null;
    }

    /**
     * 获取注解的所有属性
     *<B>说明：</B>
     *<pre>
     *  略
     *</pre>
     */
    public static Map<String,Object> getAnnotationAttrs(Annotation annotation)
    {
        Map<String,Object> attrs = new HashMap<>();
        Method[] methods = annotation.annotationType().getDeclaredMethods();
        for (Method method:methods) {
            try {
                attrs.put(method.getName(),method.invoke(annotation));
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }

        return attrs;
    }

}
